package com.sumainfor.zuul.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.collect.Lists;
import com.sumainfor.common.annotation.SysLog;
import com.sumainfor.common.util.R;
import com.sumainfor.zuul.entity.SysRoleDeptEntity;
import com.sumainfor.zuul.entity.SysRoleEntity;
import com.sumainfor.zuul.service.SysRoleDeptService;
import com.sumainfor.zuul.service.SysRoleMenuService;
import com.sumainfor.zuul.service.SysRoleService;
import com.sumainfor.zuul.validator.ValidatorUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 角色管理
 */
@RestController
@RequestMapping("/sys/role")
public class SysRoleController extends AbstractController {

    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysRoleMenuService sysRoleMenuService;
    @Autowired
    private SysRoleDeptService sysRoleDeptService;


    /**
     * 角色列表
     */
    @GetMapping("/list")
    @RequiresPermissions("sys:role:list")
    public R list(Page page, @RequestParam Map<String, Object> params) {
        return R.ok().put("page", sysRoleService.queryPage(page, params));
    }

    /**
     * 角色列表
     */
    @GetMapping("/select")
    @RequiresPermissions("sys:role:select")
    public R select(String deptId) {
        List<SysRoleEntity> list;
        if (StringUtils.isNotBlank(deptId)) {
            EntityWrapper wrapper = new EntityWrapper();
            wrapper.eq("dept_id", deptId);
            List<SysRoleDeptEntity> ids = sysRoleDeptService.selectList(wrapper);
            if (ids != null && !ids.isEmpty()) {
                list = sysRoleService.selectBatchIds(ids.stream().map(item -> item.getRoleId())
                        .collect(Collectors.toList()));
            } else {
                list = Lists.newArrayList();
            }

        } else {
            list = sysRoleService.selectList(null);
        }

        return R.ok().put("list", list);
    }

    /**
     * 角色信息
     */
    @GetMapping("/info/{roleId}")
    @RequiresPermissions("sys:role:info")
    public R info(@PathVariable("roleId") Integer roleId) {
        SysRoleEntity role = sysRoleService.getRoleById(roleId);
        if (role == null) {
            return R.error("没有查询到");
        }
        //查询角色对应的菜单
        List<Integer> menuIdList = sysRoleMenuService.queryMenuIdList(roleId);
        role.setMenuIdList(menuIdList);

        //查询对应的部门ID列表
        List<Integer> deptIdList = sysRoleDeptService.selectDeptIdList(roleId);
        role.setDeptIdList(deptIdList);

        return R.ok().put("role", role);
    }

    /**
     * 保存角色
     */
    @SysLog("保存角色")
    @PostMapping("/save")
    @RequiresPermissions("sys:role:save")
    public R save(@RequestBody SysRoleEntity role) {
        ValidatorUtils.validateEntity(role);
        logger.info("保存 角色 ====> {}", role);
        sysRoleService.save(role);

        return R.ok();
    }

    /**
     * 修改角色
     */
    @SysLog("修改角色")
    @PostMapping("/update")
    @RequiresPermissions("sys:role:update")
    public R update(@RequestBody SysRoleEntity role) {
        ValidatorUtils.validateEntity(role);

        sysRoleService.update(role);

        return R.ok();
    }

    /**
     * 删除角色
     */
    @SysLog("删除角色")
    @PostMapping("/delete")
    @RequiresPermissions("sys:role:delete")
    public R delete(@RequestBody Integer[] roleIds) {
        sysRoleService.deleteBatch(roleIds);

        return R.ok();
    }
}
